@page
@using CKY.AgentPlatform.Web.Pages.Caching
@model IndexModel
@{
    Layout = "~/Pages/Index.cshtml";
}
@using Microsoft.AspNetCore.Mvc.Localization
@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Modal
@inject IHtmlLocalizer<AgentPlatformResource> L

<div class="container-fluid">
    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">缓存管理</h1>
        <div class="btn-toolbar mb-2 mb-md-0">
            <div class="btn-group me-2">
                <button type="button" class="btn btn-sm btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#refreshConfigModal">
                    <i class="fas fa-sync-alt me-1"></i> 刷新配置
                </button>
                <button type="button" class="btn btn-sm btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#warmUpCacheModal">
                    <i class="fas fa-fire me-1"></i> 预热缓存
                </button>
            </div>
        </div>
    </div>

    <!-- 缓存状态 -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">缓存状态</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-3">
                            <div class="alert alert-success" role="alert">
                                <h6 class="alert-heading">总体状态</h6>
                                <p class="mb-0">
                                    <i class="fas fa-heartbeat me-1"></i>
                                    @(Model.CacheStatus?.OverallHealth == true ? "健康" : "异常")
                                </p>
                                <small>最后检查: @(Model.CacheStatus?.LastCheck.ToString("yyyy-MM-dd HH:mm:ss") ?? "未知")</small>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="alert alert-info" role="alert">
                                <h6 class="alert-heading">分布式缓存</h6>
                                <p class="mb-0">
                                    <i class="fas fa-database me-1"></i>
                                    @(Model.CacheStatus?.DistributedCacheHealthy == true ? "正常" : "异常")
                                </p>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="alert alert-info" role="alert">
                                <h6 class="alert-heading">Redis缓存</h6>
                                <p class="mb-0">
                                    <i class="fas fa-server me-1"></i>
                                    @(Model.CacheStatus?.RedisHealthy == true ? "正常" : "异常")
                                </p>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="alert alert-warning" role="alert">
                                <h6 class="alert-heading">命中率</h6>
                                <p class="mb-0">
                                    <i class="fas fa-percentage me-1"></i>
                                    @(Model.CacheStatistics?.CacheHitRate.ToString("P2") ?? "0%")
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 缓存统计信息 -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">缓存统计信息</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4">
                            <div class="text-center">
                                <h3 class="text-primary">@(Model.CacheStatistics?.TotalOperations.ToString("N0") ?? "0")</h3>
                                <p class="text-muted">总操作数</p>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="text-center">
                                <h3 class="text-success">@(Model.CacheStatistics?.CacheHitRate.ToString("P2") ?? "0%")</h3>
                                <p class="text-muted">缓存命中率</p>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="text-center">
                                <h3 class="text-info">@(Model.CacheStatistics?.RedisStatistics?.KeysCount.ToString("N0") ?? "0")</h3>
                                <p class="text-muted">缓存键数量</p>
                            </div>
                        </div>
                    </div>
                    
                    @if (Model.CacheStatistics?.RedisStatistics != null)
                    {
                        <div class="row mt-3">
                            <div class="col-md-6">
                                <h6>Redis 内存使用</h6>
                                <div class="progress">
                                    <div class="progress-bar bg-info" role="progressbar" 
                                         style="width: @(Model.CacheStatistics.RedisStatistics.UsedMemory > 0 && Model.CacheStatistics.RedisStatistics.MaxMemory > 0 ? (Model.CacheStatistics.RedisStatistics.UsedMemory * 100 / Model.CacheStatistics.RedisStatistics.MaxMemory) : 0)%"
                                         aria-valuenow="@(Model.CacheStatistics.RedisStatistics.UsedMemory > 0 && Model.CacheStatistics.RedisStatistics.MaxMemory > 0 ? (Model.CacheStatistics.RedisStatistics.UsedMemory * 100 / Model.CacheStatistics.RedisStatistics.MaxMemory) : 0)" 
                                         aria-valuemin="0" aria-valuemax="100">
                                        @(Model.CacheStatistics.RedisStatistics.UsedMemory > 0 && Model.CacheStatistics.RedisStatistics.MaxMemory > 0 ? (Model.CacheStatistics.RedisStatistics.UsedMemory * 100 / Model.CacheStatistics.RedisStatistics.MaxMemory) : 0)%
                                    </div>
                                </div>
                                <small class="text-muted">
                                    @(Model.CacheStatistics.RedisStatistics.UsedMemoryFormatted) / @(Model.CacheStatistics.RedisStatistics.MaxMemoryFormatted)
                                </small>
                            </div>
                            <div class="col-md-6">
                                <h6>连接客户端</h6>
                                <p class="mb-0">
                                    <i class="fas fa-users me-1"></i>
                                    @(Model.CacheStatistics.RedisStatistics.ConnectedClients) 个客户端
                                </p>
                            </div>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>

    <!-- 慢查询日志 -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">慢查询日志</h5>
                </div>
                <div class="card-body">
                    <form method="post" class="mb-3">
                        <div class="row align-items-end">
                            <div class="col-md-6">
                                <label asp-for="SlowQueryCount" class="form-label">显示数量</label>
                                <input type="number" asp-for="SlowQueryCount" class="form-control" min="1" max="1000" />
                            </div>
                            <div class="col-md-6">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fas fa-search me-1"></i> 查询
                                </button>
                            </div>
                        </div>
                    </form>
                    
                    @if (Model.SlowQueries != null && Model.SlowQueries.Any())
                    {
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead>
                                    <tr>
                                        <th>时间</th>
                                        <th>操作</th>
                                        <th>执行时间(ms)</th>
                                        <th>查询</th>
                                        <th>用户代理</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var log in Model.SlowQueries)
                                    {
                                        <tr>
                                            <td>@log.Timestamp.ToString("yyyy-MM-dd HH:mm:ss")</td>
                                            <td><span class="badge bg-warning">@log.Operation</span></td>
                                            <td><span class="badge bg-danger">@log.ExecutionTime</span></td>
                                            <td><code>@log.Query</code></td>
                                            <td><small>@log.UserAgent</small></td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    }
                    else
                    {
                        <div class="text-center text-muted">
                            <i class="fas fa-info-circle fa-2x mb-2"></i>
                            <p>暂无慢查询日志</p>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>

    <!-- 缓存键管理 -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">缓存键管理</h5>
                </div>
                <div class="card-body">
                    <form method="post" class="mb-3">
                        <div class="row align-items-end">
                            <div class="col-md-6">
                                <label asp-for="SearchPattern" class="form-label">搜索模式</label>
                                <input type="text" asp-for="SearchPattern" class="form-control" placeholder="使用 * 作为通配符" />
                            </div>
                            <div class="col-md-6">
                                <button type="submit" asp-page-handler="SearchKeys" class="btn btn-primary me-2">
                                    <i class="fas fa-search me-1"></i> 搜索
                                </button>
                                <button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#clearCacheModal">
                                    <i class="fas fa-trash me-1"></i> 清除缓存
                                </button>
                            </div>
                        </div>
                    </form>
                    
                    @if (Model.CacheKeys != null && Model.CacheKeys.Any())
                    {
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead>
                                    <tr>
                                        <th>缓存键</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var key in Model.CacheKeys)
                                    {
                                        <tr>
                                            <td><code>@key</code></td>
                                            <td>
                                                <form method="post" class="d-inline">
                                                    <input type="hidden" name="key" value="@key" />
                                                    <button type="submit" asp-page-handler="GetCacheValue" class="btn btn-sm btn-info">
                                                        <i class="fas fa-eye me-1"></i> 查看
                                                    </button>
                                                    <button type="submit" asp-page-handler="ClearCache" class="btn btn-sm btn-danger">
                                                        <i class="fas fa-trash me-1"></i> 删除
                                                    </button>
                                                </form>
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    }
                    else
                    {
                        <div class="text-center text-muted">
                            <i class="fas fa-key fa-2x mb-2"></i>
                            <p>暂无缓存键</p>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>

    <!-- 缓存值详情 -->
    @if (Model.SelectedCacheValue != null)
    {
        <div class="row mb-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">缓存值详情 - @Model.SelectedCacheValue.Key</h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6">
                                <p><strong>键:</strong> @Model.SelectedCacheValue.Key</p>
                                <p><strong>存在:</strong> @(Model.SelectedCacheValue.Exists ? "是" : "否")</p>
                                <p><strong>过期时间:</strong> @(Model.SelectedCacheValue.Expiration?.ToString("yyyy-MM-dd HH:mm:ss") ?? "永不过期")</p>
                            </div>
                            <div class="col-md-6">
                                <p><strong>检查时间:</strong> @Model.SelectedCacheValue.Timestamp.ToString("yyyy-MM-dd HH:mm:ss")</p>
                            </div>
                        </div>
                        
                        @if (Model.SelectedCacheValue.Exists)
                        {
                            <div class="mt-3">
                                <h6>值内容</h6>
                                <pre><code>@(Model.SelectedCacheValue.Value?.ToString() ?? "null")</code></pre>
                            </div>
                        }
                    </div>
                </div>
            </div>
        </div>
    }
</div>

<!-- 清除缓存确认模态框 -->
<abp-modal id="clearCacheModal" title="清除缓存" size="Large">
    <form method="post">
        <div class="mb-3">
            <label class="form-label">清除选项</label>
            <div class="form-check">
                <input class="form-check-input" type="radio" name="clearType" id="clearAll" value="all" checked>
                <label class="form-check-label" for="clearAll">
                    清除所有缓存
                </label>
            </div>
            <div class="form-check">
                <input class="form-check-input" type="radio" name="clearType" id="clearPattern" value="pattern">
                <label class="form-check-label" for="clearPattern">
                    清除指定模式的缓存
                </label>
            </div>
            <input type="text" class="form-control mt-2" id="clearPatternText" placeholder="输入模式，如 temp:*" style="display: none;" />
        </div>
        
        <abp-button type="submit" button-type="Primary" text="确认清除" 
                   icon="trash" form-action="ClearCache" />
    </form>
</abp-modal>

<!-- 刷新配置模态框 -->
<abp-modal id="refreshConfigModal" title="刷新配置">
    <p>确定要刷新缓存配置吗？这将重新应用所有缓存设置。</p>
    <form method="post">
        <abp-button type="submit" button-type="Primary" text="确认刷新" 
                   icon="sync-alt" form-action="RefreshConfig" />
    </form>
</abp-modal>

<!-- 预热缓存模态框 -->
<abp-modal id="warmUpCacheModal" title="预热缓存">
    <p>确定要预热缓存吗？这将加载常用数据到缓存中。</p>
    <form method="post">
        <abp-button type="submit" button-type="Primary" text="确认预热" 
                   icon="fire" form-action="WarmUpCache" />
    </form>
</abp-modal>

<script>
    // 显示/隐藏模式输入框
    document.addEventListener('DOMContentLoaded', function() {
        const clearPatternRadio = document.getElementById('clearPattern');
        const clearPatternText = document.getElementById('clearPatternText');
        
        clearPatternRadio.addEventListener('change', function() {
            clearPatternText.style.display = this.checked ? 'block' : 'none';
        });
        
        document.getElementById('clearAll').addEventListener('change', function() {
            clearPatternText.style.display = 'none';
        });
    });
</script>